<?php
/**
 * Created by PhpStorm.
 * User: Zhoujida
 * Date: 2016/5/26
 * Time: 16:14
 */
namespace Mengwu\Controller;

use Admin\Api\MemberApi;
use Bbs\Api\ForumPostLikeApi;
use Mengwu\Api\MwApi;
use Mengwu\Api\YuerjingReplyViewApi;
use Shop\Api\MemberConfigApi;
use Shop\Api\MmzWinnerPepoleApi;
use Shop\Api\OrderCommentApi;
use Shop\Api\OrderRefundApi;
use Shop\Api\OrdersApi;
use Shop\Api\OrdersExpressApi;
use Shop\Api\OrdersInfoViewApi;
use Shop\Api\OrdersItemApi;
use Shop\Api\WalletApi;
use Shop\Api\WalletApplyApi;
use Shop\Api\WithdrawAccountApi;
use Ucenter\Api\MemberFocusApi;
use Ucenter\Api\UserMemberApi;

class UserController extends BaseStoreController{
    protected function _initialize(){
        parent::_initialize();
        $this->jump2login();
    }

    public function index(){
        $this->assignTitle($this->userinfo['nickname'].'的个人中心');
        $uid = $this->getUID();
        //取各分类数量
        $status_count = [];
        $data = [
            'uid' => $uid,
            'page_no' => 1,
            'page_size' => 10,
            'status' => 1
        ];

        $result = mwApi(MwApi::BY_Orders_query,$data);
        $status_count[1] = $result['status'] ? $result['info']['count'] : 0;

        $data['status'] = 3;
        $result = mwApi(MwApi::BY_Orders_query,$data);
        $status_count[3] = $result['status'] ? $result['info']['count'] : 0;

        $data['status'] = 5;
        $result = mwApi(MwApi::BY_Orders_query,$data);
        $status_count[5] = $result['status'] ? $result['info']['count'] : 0;

        $data['status'] = 6;
        $result = mwApi(MwApi::BY_Orders_query,$data);
        $status_count[6] = $result['status'] ? $result['info']['count'] : 0;
        
        $this->assign('status_count',$status_count);

        //获取VIP等级
        $vip = '萌娃';
        $result = apiCall(UserMemberApi::GET_INFO,[['uid'=>$uid]]);
        if($result['status']){
            if($result['info']['group_id']==2) $vip = '萌主';
        }
        $this->assign('vip',$vip);

        //获取关注数
        $result = apiCall(MemberFocusApi::COUNT,[['focus_uid'=>$uid]]);
        if($result['status']){
            $this->assign('fans_num',$result['info']);
        }

        //获取粉丝数
        $result = apiCall(MemberFocusApi::COUNT,[['uid'=>$uid]]);
        if($result['status']){
            $this->assign('focus_num',$result['info']);
        }

        $this->boye_display();
    }

    public function order(){

        $this->assignTitle('我的订单');
        //获取订单
        $uid = $this->getUID();

        $page_size = 10;
        $p = I('get.p',1);

        $p = $p <1 ? 1: $p;
        $status = I('get.status',0);

        /**
         *  0: 全部订单 1: 待付款 3: 待收货 6: 待评价
         */
        if(!in_array($status,[0,1,3,6])) $this->display_404();

        $this->assign('status',$status);
        $this->assign('p',$p);

        $data = [
            'uid' => $uid,
            'page_no' => $p,
            'page_size' => $page_size,
            'status' => $status
        ];
        $result = mwApi(MwApi::BY_Orders_query,$data);

        if($result['status']){

            $list = $result['info']['list'];

            //$status_desc = ['','等待支付','等待发货','等待收货','已收货','退款/售后','等待评价'];

            foreach($list as &$val){
                $val['items_count'] = count($val['items']);

                $this->get_order_status_desc($val);

                if($val['order_status']==8) $val['status_desc'] = '交易关闭';

                if($val['order_status']==12) $val['status_desc'] = '订单退回';
            }

            $show = paging($p,$page_size,$result['info']['count'],'mengwu/user/order');
            $this->assign('show',$show);

            //取各分类数量
            $status_count = [];
            $data = [
                'uid' => $uid,
                'page_no' => 1,
                'page_size' => $page_size,
                'status' => 1
            ];

            $result = mwApi(MwApi::BY_Orders_query,$data);
            $status_count[1] = $result['status'] ? $result['info']['count'] : 0;

            /*$data['status'] = 2;
            $result = mwApi(MwApi::BY_Orders_query,$data);
            $status_count[2] = $result['status'] ? $result['info']['count'] : 0;*/

            $data['status'] = 3;
            $result = mwApi(MwApi::BY_Orders_query,$data);
            $status_count[3] = $result['status'] ? $result['info']['count'] : 0;

            $data['status'] = 6;
            $result = mwApi(MwApi::BY_Orders_query,$data);
            $status_count[6] = $result['status'] ? $result['info']['count'] : 0;


            $this->assign('status_count',$status_count);
            $this->assign('order_list',$list);

            $this->boye_display();
        }else{
            $this->errorPage($result['info']);
        }

    }

    /**
     *获取订单状态描述
     */
    private function get_order_status_desc(&$val){
        $status_desc = ['','等待支付','等待发货','等待收货','已收货','退款/售后','等待评价','已完成'];
        $order_status = $val['order_status'];
        $pay_status = $val['pay_status'];
        $cs_status = $val['cs_status'];
        $comment_status = $val['comment_status'];

        if($pay_status==0 && $order_status==2){
            $val['status_desc'] = $status_desc[1];
            $val['status_ss'] = 1;
        }
        elseif($pay_status==1 && in_array($order_status,[2,3])){
            $val['status_desc'] = $status_desc[2];
            $val['status_ss'] = 2;
        }
        elseif($order_status==4){
            $val['status_desc'] = $status_desc[3];
            $val['status_ss'] = 3;
        }
        elseif(in_array($cs_status,[2,3])){
            $val['status_desc'] = $status_desc[5];
            $val['status_ss'] = 5;
        }
        elseif($pay_status==1 && $order_status==5 && $comment_status==0){
            $val['status_desc'] = $status_desc[6];
            $val['status_ss'] = 6;
        }elseif($pay_status==1 && $order_status==5 && $comment_status!=0){
            $val['status_desc'] = $status_desc[7];
            $val['status_ss'] = 7;
        }else{
            $val['status_desc'] = $status_desc[0];
            $val['status_ss'] = 0;
        }

    }

    public function order_detail(){

        $this->assignTitle('订单详情');

        $order_code = I('get.order_code',"");
        $order_id = I('get.order_id',"");

        //查询订单详情
        $data = [];
        if(empty($order_id)) $data['order_code'] = $order_code;
        if(empty($order_code)) $data['oid'] = $order_id;

        $result = mwApi(MwApi::BY_Orders_detail,$data);

        if($result['status']){

            $detail = $result['info'];
            if($detail['uid']!=$this->getUID()) $this->errorPage('该订单不存在');//验证用户与订单的关联性

            $detail['items_count'] = count($detail['items']);


            //$status_desc = ['','等待支付','等待发货','等待收货','已收货','退款/售后','等待评价'];

            $this->get_order_status_desc($detail);

            if($detail['order_status']==8) $detail['status_desc'] = '交易关闭';

            if($detail['order_status']==12) $detail['status_desc'] = '订单退回';

            $this->assign('detail',$detail);

            if($detail['status_desc']=='等待支付'){
                $count_down_time = $detail['updatetime'] + 3600*24*3; //3天自动取消订单
                $this->assign('count_down_time',$count_down_time);
                $this->boye_display('User/order_detail:status_pay');
            }
            elseif(in_array($detail['status_desc'],['交易关闭','订单退回'])){
                $this->boye_display('User/order_detail:status_close');
            }else{

                if($detail['status_desc']=='等待收货'){
                    //查询物流信息
                    $result = apiCall(OrdersExpressApi::GET_INFO,[['order_code'=>$order_code]]);
                    if($result['status']){
                        $this->assign('express_info',$result['info']);
                    }
                    $count_down_time = $detail['updatetime'] + 3600*24*7; //7天自动收货
                    $this->assign('count_down_time',$count_down_time);
                }
                if($detail['status_desc']=='等待评价'){
                    $count_down_time = $detail['updatetime'] + 3600*24*15;//15天自动评价
                    $this->assign('count_down_time',$count_down_time);
                }
                
                $this->boye_display('User/order_detail:status_normal');
            }

        }else{
            $this->errorPage($result['info']);
        }

    }

    /**
     * 订单评价
     */
    public function order_pingjia(){

        $this->assignTitle('订单评价');

        //查询订单详情
        $order_code = I('get.order_code',"");
        if(empty($order_code)){
            $this->errorPage('error code');
        }

        //查询订单是否已评价
        $result = apiCall(OrdersApi::GET_INFO,[['order_code'=>$order_code]]);
        if($result['status']){
            if(is_null($result['info'])){
                $this->errorPage('该订单无效');
            }else{
                if($result['info']['comment_status']!=0){
                    $this->errorPage('该订单已经评价过了');
                }
            }
        }else{
            $this->errorPage($result['info']);
        }


        $data = ['order_code'=>$order_code];

        $result = mwApi(MwApi::BY_Orders_detail,$data);

        if($result['status']){
            $PICTURE_REMOTE_URL = C('PICTURE_REMOTE_URL');
            $detail = $result['info'];
            $items = [];
            foreach($detail['items'] as $val){
                $items[] = [
                    'id' => $val['p_id'],
                    'name' => $val['name'],
                    'img' => $PICTURE_REMOTE_URL.$val['img'].'&size=120',
                    'psku_id' => $val['psku_id'],
                    'group_id' => $val['group_id'],
                    'package_id' => $val['package_id']
                ];
            }
            $this->assign('items',json_encode($items));
            $this->assign('order_code',$order_code);
        }else{
            $this->errorPage($result['info']);
        }

        $this->boye_display();
    }

    /**
     * 评价成功
     */
    public function order_pingjia_suc(){
        $this->assignTitle('评价成功');

        $this->boye_display();
    }

    public function info(){

        $this->assignTitle('基本信息');

        $this->updateUserInfo();

        $user_info = [
            'id' => $this->userinfo['id'],
            'username' => $this->userinfo['username'],
            'nickname' => $this->userinfo['nickname'],
            'head' => $this->userinfo['head'],
            'sex' => $this->userinfo['sex'],
            'sign' => $this->userinfo['sign']
        ];

        $this->assign('user_info',$user_info);
        
        if(IS_POST){
            $nickname = I('nickname');
            $sex = I('sex');
            $sign = I('sign');

            $data = [
                'uid' => $this->getUID(),
                'nickname' => $nickname,
                'sex' => $sex,
                'sign' => $sign
            ];

            $result = mwApi(MwApi::BY_User_update,$data);

            if($result['status']){

                $this->assign('result','修改成功!');
            }else{
                $this->assign('result',$result['info']);
            }

        }
        
        $this->boye_display();
    }

    /**
     * 头像设置
     */
    public function sethead(){

        $this->updateUserInfo();

        $this->assign('head',$this->userinfo['head']);

        $this->boye_display();

    }

    public function address(){
        $this->assignTitle('收货地址');
        //取省市区地址列表
//        $result = apiCall(ProvinceApi::QUERY_NO_PAGING,array());
//        $province = $result['info'];
//
//
//
//        $result = apiCall(AreaApi::QUERY_NO_PAGING,array());


        /*{
            value: 'zhejiang',
            label: '浙江',
            children: [{
            value: 'hangzhou',
                label: '杭州',
                children: [{
                value: 'xihu',
                    label: '西湖'
                }]
            }]
        }*/

        /*$address = array();

        foreach($province as $p){

            $dizhi = [
                'value' => $p['provinceid'],
                'label' => $p['province'],
            ];

            $map = array('father' => $p['provinceid']);
            $result = apiCall(CityApi::QUERY_NO_PAGING,array($map));
            $city = $result['info'];
            foreach($city as $c){
                $v = [
                    'value' => $c['cityid'],
                    'label' => $c['city']
                ];
                $map = array('father' => $c['cityid']);
                $result = apiCall(AreaApi::QUERY_NO_PAGING,array($map));
                $area = $result['info'];
                foreach($area as $a){
                    $s = [
                        'value' => $a['areaid'],
                        'label' => $a['area']
                    ];
                    $v['children'][] = $s;
                }

                $dizhi['children'][] = $v;
            }

            $address[] = $dizhi;


        }*/


        //exit(json_encode($address));


        $this->boye_display();
    }

    public function security(){

        $mobile = $this->userinfo['mobile'];
        if(!empty($mobile)){
            $mobile = substr_replace($mobile,'****',3,4);
            $this->assign('mobile',$mobile);
        }
        $this->assignTitle('安全中心');

        $this->boye_display();
    }

    public function setpsw(){

        $this->assignTitle('修改密码');

        if(IS_POST){
            $data = [
                'uid' => $this->getUID(),
                'old_psw' => I('psw'),
                'psw' => I('new_psw')
            ];

            $result = mwApi(MwApi::BY_User_updatePsw, $data);
            if($result['status']){
                $this->assign('result','修改成功!');
            }else{
                $this->assign('result',$result['info']);
            }
        }
        $this->boye_display();
    }

    public function aftersale(){

        $page = ['curpage'=>I('p',1),'size'=>10];
        $map = ['uid'=>$this->getUID()];
        $field = '*,itboye_order_refund.id as refund_id';
        $result = apiCall(OrderRefundApi::QUERY_WITH_COUNT,[$map,$page,false,false,$field]);
        if($result['status']){
            $page_no = I('p',1);
            $page_size = 10;
            $this->assign('list',$result['info']['list']);
            $show = paging($page_no,$page_size,$result['info']['count'],'mengwu/user/aftersale');
            $this->assign('show',$show);
        }

        $this->assignTitle('我的售后');



        $this->boye_display();
    }
    public function aftersale_detail(){

        $code = I('id','int');
        if(empty($code) || strlen($code)<=14){
            $this->errorPage('无效售后编号');
        }
        $id = substr($code,14,strlen($code)-14);

        $result = apiCall(OrderRefundApi::GET_INFO,[['id'=>$id]]);
        if($result['status']){
            if(is_null($result['info'])) $this->errorPage('无效售后编号');
        }else{
            $this->errorPage($result['info']);
        }

        $sale_info = $result['info'];
        $order_code = $sale_info['order_code'];
        $result = apiCall(OrdersInfoViewApi::GET_INFO,[['order_code'=>$order_code]]);
        if($result['status']){
            if(is_null($result['info'])) $this->errorPage('无效订单');
        }else{
            $this->errorPage($result['info']);
        }

        $order_info = $result['info'];
        
        $result = apiCall(OrdersItemApi::QUERY_NO_PAGING,[['order_code'=>$order_code]]);
        if($result['status']){
            if(is_null($result['info'])) $this->errorPage('无效订单');
        }else{
            $this->errorPage($result['info']);
        }
        $item_info = $result['info'];

        $this->assign('sale_code',$code);
        $this->assign('sale_info',$sale_info);
        $this->assign('order_info',$order_info);
        $this->assign('item_info',$item_info);

        $this->assignTitle('售后详情');

        $this->boye_display();
    }
    
    public function wallet($t='mengbi'){
        $method = __FUNCTION__.'_'.$t;
        if(method_exists($this,$method)){
            $this->$method();
        }else{
            $this->display_404();
        }

    }

    private function wallet_mengbi(){
        $this->assignTitle('我的萌币');

        $map = [
            'uid' => $this->getUID()
        ];
        $result = apiCall(MemberApi::GET_INFO,[$map]);
        
        if($result['status']){
            $this->assign('mengbi',$result['info']['score']);
        }else{
            $this->errorPage($result['info']);
        }

        $this->boye_display('User/wallet:mengbi');

    }
    private function wallet_red_envelope(){
        $this->assignTitle('我的红包');

        $data = [
            'api_ver' => 101,
            'uid' => $this->getUID(),
            'is_use' => -1,
            'is_expire' => -1
        ];

        $result = mwApi(MwApi::BY_RedEnvelope_query,$data);

        $data_info = [];

        if($result['status']){
            $data_info = $result['info'];
        }else{
            $this->errorPage($result['info']);
        }

        $this->assign('data_info',json_encode($data_info));
        $this->assign('count',count($data_info));

        $this->boye_display('User/wallet:red_envelope');
    }
    private function wallet_cash_coupon(){
        $this->assignTitle('我的优惠券');

        $data = [
            'uid' => $this->getUID()
        ];

        $result = mwApi(MwApi::BY_Coupon_query,$data);

        $data_info = [];

        if($result['status']){
            $data_info = $result['info'];
        }else{
            $this->errorPage($result['info']);
        }

        $this->assign('data_info',json_encode($data_info));
        $this->assign('count',count($data_info));

        $this->boye_display('User/wallet:cash_coupon');
    }
    private function wallet_money(){
        $this->assignTitle('我的余额');

        $uid = $this->getUID();
        //查询钱包
        $result = apiCall(WalletApi::GET_INFO_If_NOT_EXIST_THEN_ADD,[$uid]);
        if($result['status']){
            $this->assign('wallet',$result['info']);
        }else{
            $this->errorPage($result['info']);
        }

        //查询提现记录
        $map = ['uid'=>$uid];
        $p = I('p',1);
        $page = ['curpage'=>$p,'size'=>10];
        $result = apiCall(WalletApplyApi::QUERY_WITH_COUNT,[$map,$page]);
        slog($result);
        if($result['status']){
            $show = paging($p,$page['size'],$result['info']['count'],'mengwu/user/wallet/t/money');
            $this->assign('show',$show);
            $apply = $result['info']['list'];

            foreach($apply as &$val){
                $map = ['uid'=>$uid];
                $result = apiCall(WithdrawAccountApi::GET_INFO,[$map]);
                if($result['status']){
                    $info = $result['info'];
                    $val['account_info'] = [
                        'account'=> $info['account'],
                        'account_type' => $info['account_type']
                    ];
                }
            }
            $this->assign('apply',$apply);
        }else{
            $this->errorPage($result['info']);
        }

        $this->boye_display('User/wallet:money');
    }
    private function wallet_bind(){
        $this->assignTitle('绑定收款账户');

        $uid = $this->getUID();

        $mobile = $this->userinfo['mobile'];
        if(empty($mobile)){
            $this->assign('result',['info'=>'未绑定手机，无法操作','url'=>U('mengwu/user/wallet/t/money')]);
        }else{
            $mobile = substr_replace($mobile,'****',3,4);
            $this->assign('mobile',$mobile);
        }

        //查找已绑定的收款账号(支付宝)
        $result = apiCall(WithdrawAccountApi::GET_INFO,[['uid'=>$uid]]);
        if($result['status']){
            if(!is_null($result['info'])){
                $account = $result['info']['account'];
                $account = substr_replace($account,'****',3,4);
                $this->assign('account',$account);
            }
        }else{
            $this->errorPage($result['info']);
        }

        if(IS_POST){

            $new_account = trim(I('account'));
            $code = trim(I('code'));
            $flag = true;
            if(empty('code')){
                $this->assign('result', ['info' => '验证码不能为空', 'url' => U('mengwu/user/wallet/t/bind')]);
                $this->boye_display('User/wallet:wallet_bind');
                return;
            }else{
                //验证验证码
                $data = [
                    'mobile' => $mobile,
                    'code_type' => 6, //绑定或修改支付宝账号
                    'code' => $code
                ];

                $result = mwApi(MwApi::BY_Message_checkCode,$data);
                if(!$result['status']){
                    $this->assign('result', ['info' => '验证码错误', 'url' => U('mengwu/user/wallet/t/bind')]);
                    $this->boye_display('User/wallet:wallet_bind');
                    return;
                }
            }
            if(empty($new_account)){
                $this->assign('result', ['info' => '收款账户不能为空', 'url' => U('mengwu/user/wallet/t/bind')]);
                $this->boye_display('User/wallet:wallet_bind');
                return;
            }
            if(!empty($account)){
                //修改
                $map = ['uid'=>$uid];
                $data = [
                    'account' => $new_account,
                ];
                $result = apiCall(WithdrawAccountApi::SAVE,[$map,$data]);
                if($result['status']){
                    $this->assign('result', ['info' => '绑定成功', 'url' => U('mengwu/user/wallet/t/money')]);
                    $this->boye_display('User/wallet:wallet_bind');
                    return;
                }else{
                    $this->assign('result', ['info' => '绑定失败', 'url' => U('mengwu/user/wallet/t/bind')]);
                    $this->boye_display('User/wallet:wallet_bind');
                    return;
                }
            }else{
                //创建
                $data = [
                    'uid' => $uid,
                    'account' => $new_account,
                    'account_type' => 1,
                    'valid_status' => 0,
                    'extra' => ''
                ];
                $result = apiCall(WithdrawAccountApi::ADD,[$data]);
                if($result['status']){
                    $this->assign('result', ['info' => '绑定成功', 'url' => U('mengwu/user/wallet/t/money')]);
                    $this->boye_display('User/wallet:wallet_bind');
                    return;
                }else{
                    $this->assign('result', ['info' => '绑定失败', 'url' => U('mengwu/user/wallet/t/bind')]);
                    $this->boye_display('User/wallet:wallet_bind');
                    return;

                }
            }

        }

        $this->boye_display('User/wallet:wallet_bind');
    }

    private function wallet_apply(){
        $this->assignTitle('申请提现');

        $uid = $this->getUID();

        //查找已绑定的收款账号(支付宝)
        $result = apiCall(WithdrawAccountApi::GET_INFO,[['uid'=>$uid]]);
        if($result['status']){
            if(!is_null($result['info'])){
                $account = $result['info']['account'];
                $account = substr_replace($account,'****',3,4);
                $this->assign('account',$account);
            }
        }else{
            $this->errorPage($result['info']);
        }

        if(IS_POST){
            $psw = I('password');
            if(empty($psw)){
                $this->assign('result', ['info' => '密码错误', 'url' => U('mengwu/user/wallet/t/apply')]);
                $this->boye_display('User/wallet:wallet_apply');
                return;
            }else{
                $data = [
                    'username' => $this->userinfo['username'],
                    'password' => $psw
                ];

                $result = mwApi(MwApi::BY_User_login,$data);
                if(!$result['status']){
                    $this->assign('result', ['info' => '密码错误', 'url' => U('mengwu/user/wallet/t/apply')]);
                    $this->boye_display('User/wallet:wallet_apply');
                    return;
                }
            }

            $money = trim(I('money'));
            if($money==''){
                $this->assign('result', ['info' => '提现金额不能为空', 'url' => U('mengwu/user/wallet/t/apply')]);
                $this->boye_display('User/wallet:wallet_apply');
                return;
            }elseif($money<=0){
                $this->assign('result', ['info' => '提现金额必须大于0', 'url' => U('mengwu/user/wallet/t/apply')]);
                $this->boye_display('User/wallet:wallet_apply');
                return;
            }else{
                $data = [
                    'uid' => $uid,
                    'reason' => '提现',
                    'valid_status' => 0,
                    'reply_msg' => '',
                    'op_id' => 0,
                    'money' => $money,
                    'account_id' => 0
                ];
                $result = apiCall(WalletApplyApi::ADD,[$data]);
                if($result['status']){
                    $this->assign('result', ['info' => '申请成功', 'url' => U('mengwu/user/wallet/t/money')]);
                    $this->boye_display('User/wallet:wallet_apply');
                    return;
                }else{
                    $this->assign('result', ['info' => '申请失败', 'url' => U('mengwu/user/wallet/t/apply')]);
                    $this->boye_display('User/wallet:wallet_apply');
                    return;
                }
            }

        }

        $this->boye_display('User/wallet:wallet_apply');
    }

    private function wallet_send_bind_sms(){
        if(IS_POST){
            $mobile = $this->userinfo['mobile'];
            if(empty($mobile)) $this->ajaxReturnErr('错误的手机号码');

            $data = [
                'mobile' => $mobile,
                'code_type' => 6 //绑定或修改支付宝账号
            ];

            $result = mwApi(MwApi::BY_Message_send_sms,$data);

            if($result['status']){
                $this->ajaxReturnSuc(['info'=>'验证码已发送~'.$result['info']]);
            }else{
                $this->ajaxReturnErr($result['info']);
            }

        }
    }


    /**
     * 售后服务
     */
    public function service(){
        $this->assignTitle('售后服务');

        //查询订单详情
        $order_code = I('get.order_code',"");
        if(empty($order_code)){
            $this->errorPage('error code');
        }
        $data = ['order_code'=>$order_code];

        $result = mwApi(MwApi::BY_Orders_detail,$data);

        if($result['status']){
            $this->assign('detail',$result['info']);
        }else{
            $this->errorPage($result['info']);
        }

        $this->boye_display();
    }

    /**
     * 提交申请售后
     */
    public function apply_service(){
        $this->assignTitle('申请售后');
        $order_code = I('get.order_code');

        $type = I('get.type',0);
        $type_desc = ['','退货退款','仅退款','换货'];
        if(in_array($type,[1,2,3])){

            $this->assign('type_desc',$type_desc[$type]);

        }else{
            $this->errorPage('错误请求');
        }

        //查询订单详情
        if(empty($order_code)){
            $this->errorPage('error code');
        }
        $data = ['order_code'=>$order_code];

        $result = mwApi(MwApi::BY_Orders_detail,$data);

        if($result['status']){
            $this->assign('detail',$result['info']);
        }else{
            $this->errorPage($result['info']);
        }

        if(IS_POST){
            
            $reason = I('reason');
            $money = I('money');
            $note =  I('note');

            $data = [
                'uid' => $this->getUID(),
                'order_code' => $order_code,
                'note' => $note,
                'reason' => $reason
            ];

            if($type == 1){
                //退货退款
                $data['refund_type'] = 3;
                $data['refund_money'] = floatval($money);
                $data['goods_get'] = 1;
            }
            if($type == 2){
                //仅退款
                $data['refund_type'] = 1;
                $data['refund_money'] = floatval($money);
                $data['goods_get'] = 2;
            }
            if($type == 3){
                //换货
                $data['refund_type'] = 2;
                $data['goods_get'] = 1;
            }

            $result = mwApi(MwApi::BY_Orders_afterSale,$data);

            if($result['status']){
                $this->assign('result','申请成功!');
            }else{
                $this->assign('result',$result['info']);
            }

        }

        $this->boye_display();
        
    }

    /**
     * 退货退款
     */
    public function return_refund(){
        $this->assignTitle('退货退款');
        $order_code = I('get.order_code');

        //查询订单详情
        if(empty($order_code)){
            $this->errorPage('error code');
        }
        $data = ['order_code'=>$order_code];

        $result = mwApi(MwApi::BY_Orders_detail,$data);

        if($result['status']){
            $this->assign('detail',$result['info']);
        }else{
            $this->errorPage($result['info']);
        }

        $this->boye_display();
    }



    public function message($t='system'){
        $method = __FUNCTION__.'_'.$t;
        if(method_exists($this,$method)){
            $this->$method();
        }else{
            $this->display_404();
        }

    }

    private function message_system(){
        $this->assignTitle('系统消息');

        //查询系统消息
        $p = I('p',1);
        $size = 10;
        $data = [
            'uid' => $this->getUID(),
            'msg_type' => 1,
            'page_size' => $size,
            'page_no' => $p
        ];
        
        $result = mwApi(MwApi::BY_Message_query,$data);
        if($result['status']){
            $show = paging($p,$size,$result['info']['count'],'mengwu/user/message/t/system');
            $this->assign('show',$show);
            $this->assign('list',$result['info']['list']);
        }else{
            $this->errorPage($result['info']);
        }



        $this->boye_display('User/message:system');
    }
    private function message_comment(){
        $this->assignTitle('评论');

        if(IS_POST){
            $p = I('page_no',1);
            $size = I('page_size',5);
            $uid = $this->getUID();
            $page = ['curpage'=>$p-1,'size'=>$size];
            $type = I('type');
            if(empty($type)){
                $this->ajaxReturnErr('error code');
            }
            $result = [];
            if($type=='receive'){
                $result = apiCall(YuerjingReplyViewApi::QUERY_WITH_UID,[$uid,$page]);
            }
            if($type=='send'){
                $result = apiCall(YuerjingReplyViewApi::QUERY_USER_COMMENT,[$uid,$page]);
            }

            if($result['status']){
                foreach($result['info']['list'] as &$val){
                    $val['author_info']['head'] = getHeadUrl($val['author_info']['head'],120);
                }
            }
            $this->apiReturn($result,null);
        }

        $this->boye_display('User/message:comment');
    }
    private function message_focus(){
        $this->assignTitle('关注');

        $uid = $this->getUID();
        //查询粉丝列表
        $p = I('p',1);
        $size = 10;
        $data = [
            'uid' => $uid,
            'msg_type' => 1,
            'page_size' => $size,
            'my_uid' => $uid,
            'page_no' => $p
        ];
        $result = mwApi(MwApi::BY_BbsUserCenter_focusMe,$data);
        if($result['status']){
            foreach($result['info']['list'] as &$val){
                $val['head'] = getHeadUrl($val['head'],120);
            }
            $show = paging($p,$size,$result['info']['count'],'mengwu/user/message/t/focus');
            $this->assign('show',$show);
            $this->assign('list',$result['info']['list']);
        }else{
            $this->errorPage($result['info']);
        }

        $this->boye_display('User/message:focus');
    }
    private function message_zan(){
        $this->assignTitle('赞');

        if(IS_POST){
            $this->needLogin();
            $map = [
                'p.author_id' => $this->getUID(),
                'fid' => ['in',[7,8]]
            ];
            $page = ['curpage'=>I('page_no',1),'size'=>I('page_size',5)];
            $result = apiCall(ForumPostLikeApi::QUERY_WITH_USER,[$map,$page]);
            if($result['status']){
                foreach($result['info']['list'] as &$val){
                    $val['head'] = getHeadUrl($val['head'],120);
                }
            }
            $this->apiReturn($result,null);

        }

        $this->boye_display('User/message:zan');
    }
    private function message_sx(){
        $this->assignTitle('私信');

        $uid = $this->getUID();
        //查询私信
        $p = I('p',1);
        $size = 10;
        $data = [
            'uid' => $uid,
            'page_size' => $size,
            'page_no' => $p
        ];
        $result = mwApi(MwApi::BY_Message_queryCall,$data);

        if($result['status']){
            foreach($result['info']['list'] as &$val){
                if(!empty($val['from_head'])){
                    $val['from_head'] = getHeadUrl($val['from_head'],120);
                }
            }
            $show = paging($p,$size,$result['info']['count'],'mengwu/user/message/t/sx');
            $this->assign('show',$show);
            $this->assign('list',$result['info']['list']);
        }else{
            $this->errorPage($result['info']);
        }

        $this->boye_display('User/message:sx');
    }

    /**
     * 换购商城
     */
    public function exchange_mall(){
        $this->display_404();
        $this->assignTitle('换购商城');
        $this->boye_display('User/exchange:mall');
    }

    /**
     * 兑换记录
     */
    public function exchange_his(){
        $this->display_404();
        $this->assignTitle('兑换记录');
        $this->boye_display('User/exchange:his');
    }

    /**
     * 兑换记录详情
     */
    public function exchange_detail(){
        $this->display_404();
        $this->assignTitle('兑换详情');
        $this->boye_display('User/exchange:detail');
    }

    /**
     * 我的收藏
     */
    public function favorite($t='sp'){
        $method = __FUNCTION__.'_'.$t;
        if(method_exists($this,$method)){
            $this->$method();
        }else{
            $this->display_404();
        }
    }
    private function favorite_sp(){
        $this->assignTitle('商铺收藏');
        $page_size = 8;
        $page_no = I('p',1);
        $data = [
            'uid' => $this->getUID(),
            'page_no' => $page_no,
            'page_size' => $page_size
        ];
        $result = mwApi(MwApi::BY_Product_getFavorites,$data);
        if($result['status']){
            $show = paging($page_no,$page_size,$result['info']['count'],'mengwu/user/favorite');
            $this->assign('show',$show);
            $list = $result['info']['list'];
            $PICTURE_REMOTE_URL = C('PICTURE_REMOTE_URL');
            foreach($list as &$val){
                $val['img'] = $PICTURE_REMOTE_URL.$val['img'].'&size=180';
            }
            $this->assign('sp_list',$list);
        }else{
            $this->errorPage($result['info']);
        }


        $this->boye_display('User/favorite:sp');
    }
    private function favorite_post(){
        $this->assignTitle('帖子收藏');
        $this->boye_display('User/favorite:post');
    }

    /**
     * 我的关注
     */
    public function focus(){
        $this->assignTitle('我的关注');

        $uid = $this->getUID();
        $p = I('p',1);
        $size = 6;
        $data = [
            'uid' => $uid,
            'msg_type' => 1,
            'page_size' => $size,
            'my_uid' => $uid,
            'page_no' => $p
        ];
        $result = mwApi(MwApi::BY_BbsUserCenter_myFocus,$data);
        if($result['status']){
            foreach($result['info']['list'] as &$val){
                $val['head'] = getHeadUrl($val['head'],120);
            }
            $show = paging($p,$size,$result['info']['count'],'mengwu/user/focus');
            $this->assign('show',$show);
            $this->assign('list',$result['info']['list']);
        }else{
            $this->errorPage($result['info']);
        }

        $this->boye_display();
    }

    /**
     * 我的粉丝
     */
    public function fans(){
        $this->assignTitle('我的粉丝');

        $uid = $this->getUID();
        $p = I('p',1);
        $size = 6;
        $data = [
            'uid' => $uid,
            'msg_type' => 1,
            'page_size' => $size,
            'my_uid' => $uid,
            'page_no' => $p
        ];
        $result = mwApi(MwApi::BY_BbsUserCenter_focusMe,$data);
        if($result['status']){
            foreach($result['info']['list'] as &$val){
                $val['head'] = getHeadUrl($val['head'],120);
            }
            $show = paging($p,$size,$result['info']['count'],'mengwu/user/fans');
            $this->assign('show',$show);
            $this->assign('list',$result['info']['list']);
        }else{
            $this->errorPage($result['info']);
        }

        $this->boye_display();
    }

    /**
     * 我的评价
     */
    public function pingjia($t='sp'){
        $method = __FUNCTION__.'_'.$t;
        if(method_exists($this,$method)){
            $this->$method();
        }else{
            $this->display_404();
        }
    }

    /**
     * 商品评价
     */
    private function pingjia_sp(){
        $this->assignTitle('商品评价');

        $uid = $this->getUID();
        //查询商品评价
        $p = I('p',1);
        $size = 10;
        $page = ['curpage'=>$p,'size'=>$size];
        $result = apiCall(OrderCommentApi::QUERY_BY_USER,[$uid,$page]);
        if($result['status']){
            $list = $result['info']['list'];
            $show = paging($p,$size,$result['info']['count'],'mengwu/user/pingjia/t/sp');
            $this->assign('show',$show);
            $this->assign('list',$list);
        }else{
            $this->errorPage($result['info']);
        }

        $this->boye_display('User/pingjia:sp');
    }

    /**
     * 赠无限评价
     */
    private function pingjia_zwx(){
        $this->assignTitle('赠无限评价');

        $uid = $this->getUID();
        //查询赠无限评价
        $p = I('p',1);
        $size = 10;
        $page = ['curpage'=>$p,'size'=>$size];
        $result = apiCall(MmzWinnerPepoleApi::QUERY_WITH_PRODUCT_BY_USER,[$uid,$page]);
        if($result['status']){
            $list = $result['info']['list'];
            $show = paging($p,$size,$result['info']['count'],'mengwu/user/pingjia/t/zwx');
            $this->assign('show',$show);
            $this->assign('list',$list);
        }else{
            $this->errorPage($result['info']);
        }
        $this->boye_display('User/pingjia:zwx');
    }

    /**
     * 帮助中心
     */
    public function help(){
        redirect(U('mengwu/helpCenter/index'));
    }

    /**
     * 百宝箱
     */
    public function baibaoxiang($t='yej'){
        $method = __FUNCTION__.'_'.$t;
        if(method_exists($this,$method)){
            $this->$method();
        }else{
            $this->display_404();
        }
    }
    /**
     * 百宝箱-育儿经
     */
    private function baibaoxiang_yej(){
        $this->assignTitle('百宝箱-育儿经');
        $this->boye_display('User/baibaoxiang:yej');
    }

    /**
     * 百宝箱-赠无限
     */
    private function baibaoxiang_zwx(){
        $this->assignTitle('百宝箱-赠无限');
        $this->boye_display('User/baibaoxiang:zwx');
    }

    /**
     * 邀请有奖
     */
    public function invite(){
        $this->assignTitle('邀请有奖');

        $uid = $this->getUID();

        $map = ['uid'=>$uid];

        $result = apiCall(MemberConfigApi::GET_INFO,[$map]);

        if($result['status']){
            $idcode = $result['info']['idcode'];
            $invite_id = $result['info']['invite_id'];
        }else{
            $this->errorPage($result['info']);
        }

        //查询邀请状态
        //1.查询是否来自邀请注册 来自邀请注册的用户 只需要邀请5个注册就能成为VPI2
        //2.查询邀请注册成功的数量

        $map = ['invite_id'=>$uid];
        $invite_count = 0;
        $result = apiCall(MemberConfigApi::COUNT,[$map]);
        if($result['status']){
            $invite_count = $result['info'];
        }else{
            $this->errorPage($result['info']);
        }
        if(empty($invite_id)){
            //不是邀请注册的用户,需要邀请10个
            if($invite_count<10){
                $tip = "您还需成功邀请注册".(10-$invite_count)."才能成为萌主~";
            }else{
                $tip = '您已经成为萌主~';
            }

        }else{
            //是邀请注册的用户，需要邀请5个
            if($invite_count<5){
                $tip = "您还需成功邀请注册".(5-$invite_count)."才能成为萌主~";
            }else{
                $tip = '您已经成为萌主~';
            }
        }
        $this->assign('tip',$tip);

        $this->assign('idcode',$idcode);

        $this->boye_display();
    }

}